package org.cbyyx.tc.controller;

import lombok.RequiredArgsConstructor;
import org.cbyyx.tc.annotation.AuthCheck;
import org.cbyyx.tc.common.BaseResponse;
import org.cbyyx.tc.common.ResultUtils;
import org.cbyyx.tc.constant.UserConstant;
import org.cbyyx.tc.enums.ErrorCode;
import org.cbyyx.tc.exception.ThrowUtils;
import org.cbyyx.tc.model.dto.addressRequest.AddressAddRequest;
import org.cbyyx.tc.model.dto.addressRequest.AddressUpdateRequest;
import org.cbyyx.tc.model.dto.addressRequest.AddressQueryRequest;
import org.cbyyx.tc.model.vo.addressResponse.AddressVO;
import org.cbyyx.tc.service.AddressService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户地址接口
 *
 * @author cbyyx
 */
@RestController
@RequestMapping("/address")
@RequiredArgsConstructor
public class AddressController {
    private final AddressService addressService;

    /**
     * 添加地址
     */
    @PostMapping
    @AuthCheck(role = UserConstant.USER)
    public BaseResponse<Boolean> addAddress(@RequestBody @Valid AddressAddRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR, "请求参数不能为空");
        return ResultUtils.success(addressService.addAddress(request));
    }

    /**
     * 更新地址
     */
    @PutMapping
    @AuthCheck(role = UserConstant.USER)
    public BaseResponse<Boolean> updateAddress(@RequestBody @Valid AddressUpdateRequest request) {
        ThrowUtils.throwIf(request == null || request.getId() == null, ErrorCode.PARAMS_ERROR, "请求参数不能为空");
        return ResultUtils.success(addressService.updateAddress(request));
    }

    /**
     * 删除地址
     */
    @DeleteMapping("/{id}")
    @AuthCheck(role = UserConstant.USER)
    public BaseResponse<Boolean> deleteAddress(@PathVariable Long id) {
        ThrowUtils.throwIf(id == null || id <= 0, ErrorCode.PARAMS_ERROR, "地址ID无效");
        return ResultUtils.success(addressService.deleteAddress(id));
    }

    /**
     * 获取地址详情
     */
    @GetMapping("/{id}")
    @AuthCheck(role = UserConstant.USER)
    public BaseResponse<AddressVO> getAddress(@PathVariable Long id) {
        ThrowUtils.throwIf(id == null || id <= 0, ErrorCode.PARAMS_ERROR, "地址ID无效");
        return ResultUtils.success(addressService.getAddress(id));
    }

    /**
     * 查询地址列表
     */
    @GetMapping("/list")
    @AuthCheck(role = UserConstant.USER)
    public BaseResponse<List<AddressVO>> listAddresses(AddressQueryRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR, "请求参数不能为空");
        return ResultUtils.success(addressService.listAddresses(request));
    }

    /**
     * 设置默认地址
     */
    @PutMapping("/default/{id}")
    @AuthCheck(role = UserConstant.USER)
    public BaseResponse<Boolean> setDefaultAddress(@PathVariable Long id) {
        ThrowUtils.throwIf(id == null || id <= 0, ErrorCode.PARAMS_ERROR, "地址ID无效");
        return ResultUtils.success(addressService.setDefaultAddress(id));
    }

    /**
     * 获取默认地址
     */
    @GetMapping("/default")
    @AuthCheck(role = UserConstant.USER)
    public BaseResponse<AddressVO> getDefaultAddress() {
        AddressVO defaultAddress = addressService.getDefaultAddress();
        return ResultUtils.success(defaultAddress);
    }
}